/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utilities;

/**
 *
 * @author zalem1
 */
public class DataFormater {
    public static String GetHumanReadableFileSize(Long sizeInBytes)
    {
        Long sizeInKB;
        Long sizeInMB;
        Long sizeInGB;
        Long sizeInTB;
        
        if (sizeInBytes < 1000)
            return sizeInBytes + " bytes";
        else if ((sizeInKB = sizeInBytes/1024) < 1000)
            return sizeInKB + " KB";
        else if ((sizeInMB = sizeInKB/1024) < 1000)
            return sizeInMB + " MB";
        else if ((sizeInGB = sizeInMB/1024) < 1000)
            return sizeInGB + " GB";
        else 
        {
            sizeInTB = sizeInGB/1024;
            return sizeInTB + " TB";
        }
        
    }

    public static String GetHumanReadableTime(Long timeInMs)
    {
        Long timeInSec;
        Long timeInMin;
        Long timeInHours;
        Long timeInDay;
        
        Long modulo;
        String unit;
        
        if (timeInMs < 1000)
            return timeInMs + " ms";
        else if ((timeInSec = timeInMs/1000) < 60) {
            modulo = timeInMs % 1000;
            unit = (timeInSec == 1) ? "second" : "seconds";
            
            if (modulo > 0)
                return timeInSec + " " + unit + " and " + GetHumanReadableTime(modulo);
            else
                return timeInSec + " " + unit;
        }
        else if ((timeInMin = timeInSec/60) < 60) {
            modulo = (timeInSec % 60) * 1000;
            unit = (timeInMin == 1) ? "min" : "mins";
            
            if (modulo > 0)
                return timeInMin + " " + unit + " and " + GetHumanReadableTime(modulo);
            else
                return timeInMin + " " + unit;
        }
        else if ((timeInHours = timeInMin/60) < 60) {
            modulo = ((timeInMin % 60) * 60 * 1000);
            unit = (timeInHours == 1) ? "hour" : "hours";
            
            if (modulo > 0)
                return timeInHours + " " + unit + " and " + GetHumanReadableTime(modulo);
            else
                return timeInHours + " " + unit;
        }
        else
        {
            timeInDay = timeInHours/24;
            modulo = ((timeInHours % 24) * 60 * 60 * 1000);
            unit = (timeInHours == 1) ? "day" : "days";
            
            if (modulo > 0)
                return timeInDay + " " + unit + " and " + GetHumanReadableTime(modulo);
            else
                return timeInDay + " " + unit;
        }

    }
}
